package com.decent.ejfadmin.collectcard.controller;

import com.decent.ejfadmin.collectcard.entity.SupplyCardViewBean;
import com.decent.ejfadmin.collectcard.service.CollectCardRecordService;
import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.bean.SupplyCard;
import com.decent.ejfadmin.user.service.UserService;
import com.decent.ejfadmin.utils.ParamUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;

/**
 * @author weicx
 * @date 2018/07/17
 */
@RestController
@RequestMapping("CardReceiveController")
@Slf4j
public class CollectCardRecordController {
    @Autowired
    private CollectCardRecordService collectCardRecordService;
    @Autowired
    private UserService userService;

    /**
     * 收卡记录列表(批次分组)
     *
     * @param startTime 查询开始时间
     * @param endTime   查询结束时间
     * @param page      当前页数
     * @return MemberMessage
     */
    @RequestMapping("/userCardSupplyRecord")
    public Page querySupplyOrderList(String startTime, String endTime, Page page, String area, SupplyCardViewBean card) {
        try {
            ParamUtil.verifyRequestParam(startTime, endTime, page);
            String userid = null;
            if (StringUtils.isNotBlank(card.getUserAccount())) {
                userid = userService.getUserId(card.getUserAccount());
                if (userid == null || StringUtils.equals("toomuch", userid)) {
                    return userService.dealUserid(userid);
                }
            }
            String[] userIds = StringUtils.isBlank(userid) ? null : userid.split(",");
            card.setArea(URLDecoder.decode(StringUtils.defaultIfBlank(area, ""), "UTF-8"));
            return collectCardRecordService.querySupplyOrderList(page, startTime, endTime, userIds, card);
        } catch (Exception e) {
            log.warn("收卡记录列表查询异常: [{}]", e, e);
            page.setResultList(new ArrayList<>());
            page.setTotal(0);
            return page;
        }
    }

    /**
     * 修改收卡状态为失败
     *
     * @param batchIds 批次号
     * @param status   状态 4:未预判面值   7:等待慢速试充
     * @return MemberMessage
     */
    @RequestMapping("setBatchFail")
    public MessageBean putCardFail(Integer status, String[] batchIds) {
        try {
            if (ArrayUtils.isEmpty(batchIds)) {
                return MessageBean.fail("批次号不可为空");
            }
            if (status == null) {
                return MessageBean.fail("请选择卡密状态");
            }
            String batchIdsStr = "'".concat(StringUtils.join(batchIds, "','")).concat("'");
            int rows = collectCardRecordService.putCardFail(batchIdsStr, status);
            return MessageBean.success("成功操作" + rows + "条记录");
        } catch (Exception e) {
            log.warn("修改收卡订单为失败状态异常: [{}]", e, e);
            return MessageBean.fail("操作失败");
        }
    }


    /**
     * 批次查询收卡列表
     *
     * @param page      分页组件
     * @param queryCard 查询bean
     * @return MemberMessage
     */
    @RequestMapping("cardDetailByBatchId")
    @ResponseBody
    public Page cardDetailByBatchId(Page page, SupplyCard queryCard) {
        if (queryCard == null) {
            page.setResultList(Collections.emptyList());
            page.setTotal(0);
            return page;
        }
        if (StringUtils.isBlank(queryCard.getStartTime()) || StringUtils.isBlank(queryCard.getEndTime())) {
            page.setQueryBean(MessageBean.fail("请输入起始时间"));
            page.setResultList(Collections.EMPTY_LIST);
            page.setTotal(0);
            return page;
        }
        return collectCardRecordService.cardDetailByBatchId(page, queryCard);
    }


}
